package com.stevetracey.backup;

import java.util.ArrayList;
import java.util.List;

/**
 * Holds all details of the job including source and destination
 * folder paths and execution statistics
 * 
 * @author stephen.m.tracey
 *
 */
public class BackupJobContext 
{
	/** the remote path of the parent folder containing the backup target folder **/
	private String remoteRootPath;
	
	/** the local path of the parent folder containing the backup target folder **/
	private String localRootPath;
	
	/** the name of the folder to be backed up **/
	private String backupFolderName;
	
	/** the count of the files that have been successfully synchronised **/
	private int successfulNewFileCount;
	
	/** the count of the files that have been successfully updated following changes detected **/
	private int successfulChangedFileCount;
	
	/** the total number of local folders scanned **/
	private int totalLocalFolders;
	
	/** the total number of local files scanned **/
	private int totalLocalFiles;
	
	private long totalLocalBytes;
	
	private long successfulNewFileBytes;
	
	private long successfulChangedFileBytes;
	
	private long failedFileBytes;

	/** Lists of failed files **/
	private List<String> failedFiles = new ArrayList<String>();
	
	private boolean running = false;
	
	public long getTotalLocalBytes() {
		return totalLocalBytes;
	}

	public void addTotalLocalBytes(long bytes) {
		this.totalLocalBytes += bytes;
	}

	public long getSuccessfulNewFileBytes() {
		return successfulNewFileBytes;
	}

	public void addSuccessfulNewFileBytes(long pBytes) {
		this.successfulNewFileBytes += pBytes;
	}

	public long getSuccessfulChangedFileBytes() {
		return successfulChangedFileBytes;
	}

	public void addSuccessfulChangedFileBytes(long pBytes) {
		this.successfulChangedFileBytes += pBytes;
	}

	public long getFailedFileBytes() {
		return failedFileBytes;
	}

	public void addFailedFileBytes(long pBytes) {
		this.failedFileBytes += pBytes;
	}

	public int getSuccessfulChangedFileCount() 
	{
		return successfulChangedFileCount;
	}

	public void incrementSuccessfulChangedFileCount() 
	{
		this.successfulChangedFileCount++;
	}
	
	public int getTotalLocalFolders() {
		return totalLocalFolders;
	}

	public void incrementTotalLocalFolders() {
		this.totalLocalFolders++;
	}

	public int getTotalLocalFiles() {
		return totalLocalFiles;
	}

	public void incrementTotalLocalFiles() {
		this.totalLocalFiles++;
	}

	
	public BackupJobContext(String pSourceFolderPath, String pRemoteDestinationPath)
	{
		setBackupDestination(pRemoteDestinationPath);
		setBackupSource(pSourceFolderPath);
	}
	
	public void setBackupDestination(String pPath)
	{
		this.remoteRootPath = pPath.replaceAll("\\\\", "/");
	}
	
	/**
	 * Sets the folder to be backed up
	 * 
	 * @param pPath
	 */
	public void setBackupSource(String pPath)
	{
		this.localRootPath = pPath.replaceAll("\\\\", "/").substring(0, pPath.lastIndexOf("/"));
		this.backupFolderName = pPath.replaceAll("\\\\", "/").substring(pPath.lastIndexOf("/")+1);
	}
	
	
	public String getRemoteRoot()
	{
		return this.remoteRootPath;
	}
	
	public String getLocalRoot()
	{
		return this.localRootPath;
	}
	
	public String getFolderName()
	{
		return this.backupFolderName;
	}
	
	public String getLocalSourceFolderPath()
	{
		return this.localRootPath + "/" + this.backupFolderName;
	}
	
	public String getRemoteFolderPath()
	{
		return this.remoteRootPath + "/" + this.backupFolderName;
	}
	
	public void incrementSuccessfulNewFileCount()
	{
		this.successfulNewFileCount++;
	}
	
	
	public void addFailedFile(String pFile)
	{
		this.failedFiles.add(pFile);
	}
	
	public List<String> getFailedFiles()
	{
		return this.failedFiles;
	}
	
	public int getSuccessfulNewFileCount()
	{
		return this.successfulNewFileCount;
	}
	
	public void setRunning(boolean pRunning)
	{
		this.running = pRunning;
	}
	
	public boolean isRunning()
	{
		return this.running;
	}
	
	/**
	 * Reset the counters between runs
	 */
	public void resetCounters()
	{
		this.successfulNewFileCount=0;
		this.successfulChangedFileCount=0;
		this.totalLocalFiles=0;
		this.totalLocalFolders=0;
		this.failedFiles.clear();				
	}
	
	/**
	 * Get a Local file path based on a remote one
	 * 
	 * @param pRemotePath
	 *            - the remote path to translate
	 * @return
	 */
	public  String getLocalPath(String pRemotePath) 
	{
		String subPath = pRemotePath.replaceAll("\\\\", "/").substring(this.remoteRootPath.length());
		if (subPath.startsWith("/")) subPath = subPath.substring(1);
		if (subPath.endsWith("/")) subPath = subPath.substring(0, subPath.length() -1);
		String path = this.localRootPath + "/" + this.backupFolderName + "/" + subPath;
		if (path.endsWith("/")) path = path.substring(0, path.length() -1);
		return path;
		

	}
	/**
	 * Get a Remote file path given a local one
	 * 
	 * @param pLocalPath
	 *            - the local path to translate
	 *            - the local base path (where is the folder that is archived)
	 * @return
	 */
	public String getRemotePath(String pLocalPath) 
	{
		String subPath = pLocalPath.replaceAll("\\\\", "/").substring(this.localRootPath.length());
		if (subPath.startsWith("/")) subPath = subPath.substring(1);
		if (subPath.endsWith("/")) subPath = subPath.substring(0, subPath.length() -1);
		String path = this.remoteRootPath + "/" + subPath;
		if (path.endsWith("/")) path = path.substring(0, path.length() -1);
		return path;
	}
	
	public String toString()
	{
		return "[" + this.getClass().getName() +": "
				+ "remoteRootPath=" + remoteRootPath + ", "
				+ "localRootPath=" + localRootPath + ", "
				+ "backupFolderName=" + backupFolderName + ", "
				+ "successfulNewFileCount=" + successfulNewFileCount + ", "
				+ "successfulChangedFileCount=" + successfulChangedFileCount + ", "
				+ "totalLocalFiles=" + totalLocalFiles + ", "
				+ "totalLocalFolders=" + totalLocalFolders + ", "
				+ "failedFiles=" + failedFiles.size() + "]";

	}
}


